package app;

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JComboBox;

import java.awt.FlowLayout;
import java.awt.Color;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import app.DrawingCanvas;

@SuppressWarnings("serial")
public class ControlPanelView extends JPanel {

	private JButton clearButton;
	
	private JLabel colorLabel;
	
	private String[] colors = { "Black", "Red", "Blue", "Green" };
	private JComboBox colorBox;
	
	private DrawingCanvas canvas;
	
	public ControlPanelView(DrawingCanvas c) {
		canvas = c;
		
		this.setLayout(new FlowLayout());
		
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new ClearHandler());
		this.add(clearButton);
		
		colorLabel = new JLabel("Select Color:");
		this.add(colorLabel);
		
		colorBox = new JComboBox(colors);
		colorBox.addItemListener(new ColorHandler());
		this.add(colorBox);
	}
	
	private class ColorHandler implements ItemListener {
		public void itemStateChanged(ItemEvent e) {
			int selected = colorBox.getSelectedIndex();
			Color newColor = canvas.getpenColor();
			if (selected == 0) {
				newColor = Color.BLACK;
			}
			else if (selected == 1) {
				newColor = Color.RED;
			}
			else if (selected == 2) {
				newColor = Color.BLUE;
			}
			else if (selected == 3) {
				newColor = Color.GREEN;
			}
			if (canvas.getActiveObject() != null) {
				canvas.getActiveObject().setColor(newColor);
				canvas.redrawAll();
			}
			canvas.setpenColor(newColor);
		}
	}
	
	private class ClearHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			canvas.clear();
		}
	}
	
}
